package com.netbetter.langzheng.plugin.fileManager.reader;

import java.util.ArrayList;
import java.util.Date;

import com.extjs.gxt.ui.client.data.BaseListLoadResult;
import com.extjs.gxt.ui.client.data.DataField;
import com.extjs.gxt.ui.client.data.DataReader;
import com.extjs.gxt.ui.client.data.ListLoadResult;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONValue;
import com.netbetter.langzheng.plugin.fileManager.model.FileModel;

/**
 * 将proxy读取到的json转换为List
 * 
 * @author 嘉陵江里捉鱼
 * @param <C>
 */
public class FileInfoReader<C> implements DataReader<C, ListLoadResult<ModelData>> {

    public ListLoadResult read(C loadConfig, Object data) {
        JSONArray root = (JSONArray) JSONParser.parse((String) data);
        int size = root.size();
        ArrayList<ModelData> records = new ArrayList<ModelData>();
        ModelType modelType = newModelType();
        for (int i = 0; i < size; i++) {
            JSONObject obj = (JSONObject) root.get(i);
            FileModel model = newModelInstance();
            for (int j = 0; j < modelType.getFieldCount(); j++) {
                DataField field = modelType.getField(j);
                String map = field.map != null ? field.map : field.name;
                JSONValue value = obj.get(map);
                if (value == null) continue;
                if (value.isArray() != null) {
                    // nothing
                } else if (value.isBoolean() != null) {
                    model.set(field.name, value.isBoolean().booleanValue());
                } else if (value.isNumber() != null) {
                    model.set(field.name, (int) value.isNumber().doubleValue());
                } else if (value.isObject() != null) {
                    // nothing
                } else if (value.isString() != null) {
                    String s = value.isString().stringValue();
                    if (field.type != null) {
                        if (field.type.equals(Date.class)) {
                            DateTimeFormat format = DateTimeFormat.getFormat(field.format);
                            Date d = format.parse(s);
                            model.set(field.name, d);
                        }
                    } else {
                        model.set(field.name, s);
                    }

                } else if (value.isNull() != null) {
                    model.set(field.name, null);
                }
            }
            // 设置icon图标
            model.setIconName(model.getName());
            // 设置格式化的数据
            model.setFSize(model.getSize());
            model.setType(model.getName());
            records.add(model);
        }
        return new BaseListLoadResult(records);
    }

    /**
     * Returns the new model instances. Subclasses may override to provide an model data subclass.
     * 
     * @return the new model data instance
     */
    protected FileModel newModelInstance() {
        return new FileModel();
    }

    protected ModelType newModelType() {
        ModelType modelType = new ModelType();
        modelType.addField("name", "file_name");
        modelType.addField("size", "file_size");
        modelType.addField("id", "id");
        modelType.addField("isDirectory", "is_directory");
        modelType.addField("createTime", "created_at");
        modelType.addField("modifyTime", "file_change_time");
        modelType.addField("accessTime", "file_access_time");
        return modelType;
    }
}
